import { useEffect, useState } from "react";
import { Modal, message } from "antd";
import { DeleteTableProps } from "../type/table";
import { Key } from "antd/es/table/interface";

const { confirm } = Modal;
export const useDeleteTable = (props: DeleteTableProps) => {
  const { API } = props;
  const [deleteProps, setDeleteProps] = useState<string[]>([]);
  useEffect(() => {
    if (deleteProps.length > 0) {
      // 初始化判断不默认执行
      deleteTable();
    }
  }, [deleteProps]);
  const deleteTable = async (currentDelId?: string[]) => {
    /**
      删除逻辑是当currentDelId值存为单删，不存在则为多删
     */
    // if(!currentDelId || currentDelId.length==0)return;
    confirm({
      title: props.title || "确定要删除是吗？",
      async onOk() {
        await API(currentDelId || deleteProps);
        message.success("删除成功");
        // setDeleteProps([])
        props.success && props.success();
      },
    });
  };
  return {
    deleteProps,
    setDeleteProps,
    deleteTable,
  };
};
